package com.sc.nft.dao;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.TradingPoolOrderSettle;
import com.sc.nft.enums.DivideTypeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.mapper.TradingPoolOrderSettleMapper;
import com.sc.nft.sup.ErrorCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 交易池订单分润结算表(sc_trading_pool_order_settle)数据DAO
 *
 * @author jinpinhai
 * @since 2024-11-01 15:12:46
 * @description Generator
 */
@Slf4j
@Repository
@RequiredArgsConstructor
public class TradingPoolOrderSettleDao extends ServiceImpl<TradingPoolOrderSettleMapper, TradingPoolOrderSettle> {

    @Repository
    static class TradingPoolOrderSettleDaoAdapter extends ServiceImpl<TradingPoolOrderSettleMapper, TradingPoolOrderSettle> {

    }

    private final TradingPoolOrderSettleDaoAdapter adapter;

    public List<TradingPoolOrderSettle> getWaitSettle() {
        // 每日由定时任务结算 is_settle = false 数据只会是近期数据，冷热分离保留2个月数据
        return adapter.lambdaQuery()
                .eq(TradingPoolOrderSettle::getDivideType, DivideTypeEnum.DAO)
                .eq(TradingPoolOrderSettle::getIsSettle, false)
                .last("limit 1000")
                .list();
    }


    public void updateSettle(Long id) {
        Assert.isTrue(adapter.lambdaUpdate()
                        .eq(TradingPoolOrderSettle::getId, id)
                        .eq(TradingPoolOrderSettle::getIsSettle, false)
                        .set(TradingPoolOrderSettle::getIsSettle, true)
                        .update(),
                () -> new GlobalRunTimeException(ErrorCode.UPDATE_ERROR));
    }

}